package com.zjsru.controller;

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import com.zjsru.pojo.Order;
import com.zjsru.pojo.User;
import com.zjsru.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/*@Controller
@ResponseBody*/
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private NacosServiceDiscovery nacosServiceDiscovery;
    @Autowired
    private RestTemplate restTemplate;
    //根据商品ID查询商品信息
   /* @GetMapping("/order/query/{id}")
    public String queryOrderById(@PathVariable("id") Long orderId) {
        return "ID为"+orderId+"订单查询成功";
    }*/
    //根据商品ID查询商品信息及用户信息
    @GetMapping("/order/query/{id}")
    public Order queryOrderById(@PathVariable("id") Long orderId) throws NacosException {
        Order order = orderService.queryOrderById(orderId);
        ServiceInstance serviceInstance  = nacosServiceDiscovery.getInstances("nacos-user").get(0);
        String url = serviceInstance.getHost()+":"+serviceInstance.getPort();
        User user = restTemplate.getForObject(url, User.class,order.getUser_Id());
        order.setUser(user);
        return order;
    }

}
